package com.young.shop.dao.impl;

import com.young.shop.dao.OrdersListDao;
import com.young.shop.pojo.entity.OrderList;
import com.young.shop.pojo.entity.OrdersDetails;
import com.young.shop.utils.DBUtils;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrdersListDaoImpl implements OrdersListDao {
//    //通过DBUtil类拿到queryRunner
//    private QueryRunner queryRunner = DBUtil.getQueryRunner();
    //创建自动化管理工具
    private static QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
    //时间格式
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //开启驼峰映射
    private static BeanProcessor bean = new GenerousBeanProcessor();
    private static RowProcessor processor = new BasicRowProcessor(bean);

    @Override
    public List<OrderList> selectById(Long Id) {
        String sql = "SELECT * FROM t_order_list WHERE user_id = ?";
        //定义返回形式
        BeanListHandler<OrderList> orderBeanListHandler = new BeanListHandler<>(OrderList.class, processor);
        //执行sql
        try {
            List<OrderList> query = queryRunner.query(sql, orderBeanListHandler, Id);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    //查询用户id与订单状态查询当前状态的所有订单
    @Override
    public List<OrderList> selectByZtId(int status, Long userId) {
        String sql = "SELECT * FROM t_order_list WHERE user_id = ? AND `status` = ?";
        //定义返回形式
        BeanListHandler<OrderList> orderBeanListHandler = new BeanListHandler<>(OrderList.class, processor);
        Object[] params = {userId,status};
        //执行sql
        try {
            List<OrderList> query = queryRunner.query(sql, orderBeanListHandler, params);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    //根据订单id修改状态值
    @Override
    public int byIdAlterstaStatus(int id, int newStatus){
        String sql ="UPDATE t_order_list SET `status` = ? , creation_time = ? WHERE id = ?";
        //获取当前时间
        String dateFormat = simpleDateFormat.format(new Date());
        Object[] params = {newStatus,dateFormat,id};
        try {
            return queryRunner.update(sql, params);
        } catch (SQLException e) {
            return 0;
        }
    }

    @Override
    public List<OrdersDetails> orderdetailByOrderId(Long Id) {
        String sql = "SELECT g.NAME,g.image_url,p.amount,g.price FROM t_goods g LEFT JOIN t_product p ON g.id = p.goods_id LEFT JOIN t_order_list a ON p.order_id = a.id WHERE a.id = ?";
        BeanListHandler<OrdersDetails> ordersDetailsBeanListHandler = new BeanListHandler<>(OrdersDetails.class, processor);
        try {
            List<OrdersDetails> query = queryRunner.query(sql, ordersDetailsBeanListHandler, Id);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
